package com.ly.heart.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ly.common.core.domain.AjaxResult;
import com.ly.common.exception.CustomException;
import com.ly.common.utils.DateUtils;
import com.ly.common.utils.SecurityUtils;
import com.ly.common.utils.StringUtils;
import com.ly.common.utils.erp.ConstUtil;
import com.ly.common.utils.erp.SnowflakeIdWorker;
import com.ly.heart.domain.KingTravelExpensesTypeEntity;
import com.ly.heart.domain.dto.StartProcessDto;
import com.ly.heart.mapper.KingTravelExpensesTypeMapper;
import com.ly.heart.service.IActModelService;
import com.ly.heart.service.IKingTravelExpensesTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 差旅费类型 服务实现类
 * </p>
 * @author WuZH
 * @since 2024-08-18
 */
@Service
public class KingTravelExpensesTypeServiceImpl extends ServiceImpl<KingTravelExpensesTypeMapper, KingTravelExpensesTypeEntity> implements IKingTravelExpensesTypeService {

    @Resource
    private IActModelService iActModelService;

    @Override
    public List<KingTravelExpensesTypeEntity> queryList (KingTravelExpensesTypeEntity kingTravelExpensesTypeEntity) {
        return null;
    }

    @Override
    public AjaxResult draft (KingTravelExpensesTypeEntity kingTravelExpensesTypeEntity) {
        kingTravelExpensesTypeEntity.setId ( SnowflakeIdWorker.getInstance ().nextStringId () );
        kingTravelExpensesTypeEntity.setCreateTime ( DateUtils.getNowDate () );
        kingTravelExpensesTypeEntity.setCreateBy ( SecurityUtils.getLoginUser ().getUser ().getUserId () );
        kingTravelExpensesTypeEntity.setState ( ConstUtil.STATE_IN_DRAFT );
        boolean save = this.save ( kingTravelExpensesTypeEntity );
        if ( ! save ) {
            throw new CustomException ( "暂存差旅费类型失败！" );
        }
        return AjaxResult.success ( "暂存差旅费类型成功", kingTravelExpensesTypeEntity.getId () );
    }

    @Override
    public AjaxResult submit (KingTravelExpensesTypeEntity kingTravelExpensesTypeEntity) {
        if ( StringUtils.isBlank ( kingTravelExpensesTypeEntity.getId () ) ) {
            return AjaxResult.error ( "请先暂存该单据！" );
        }
        StartProcessDto startDto = new StartProcessDto ().
                setBusinessId ( kingTravelExpensesTypeEntity.getId () ).
                setBusinessType ( kingTravelExpensesTypeEntity.getBusinessType () ).setModelId ( kingTravelExpensesTypeEntity.getModelId () ).
                setBusinessName ( "发票与费用项目设置".concat ( kingTravelExpensesTypeEntity.getId () ) );
        String instanceId = iActModelService.startProcess ( startDto );
        boolean update = this.update ( new LambdaUpdateWrapper<KingTravelExpensesTypeEntity> ().
                set ( KingTravelExpensesTypeEntity :: getUpdateBy, SecurityUtils.getLoginUser ().getUser ().getUserId () ).
                set ( KingTravelExpensesTypeEntity :: getUpdateTime, DateUtils.getNowDate () ).
                set ( KingTravelExpensesTypeEntity :: getInstanceId, instanceId ).
                set ( KingTravelExpensesTypeEntity :: getState, ConstUtil.STATE_IN_PROCESS ) );
        if ( ! update ) {
            throw new CustomException ( "提交差旅费类型失败" );
        }
        return AjaxResult.success ( "提交差旅费类型成功" );
    }

    @Override
    public AjaxResult updateSingle (KingTravelExpensesTypeEntity kingTravelExpensesTypeEntity) {
        kingTravelExpensesTypeEntity.setUpdateBy ( SecurityUtils.getLoginUser ().getUser ().getUserId () );
        kingTravelExpensesTypeEntity.setUpdateTime ( DateUtils.getNowDate () );
        boolean b = this.updateById ( kingTravelExpensesTypeEntity );
        if ( ! b ) {
            throw new CustomException ( "修改差旅费类型失败！" );
        }
        return AjaxResult.success ( "修改差旅费类型成功" );
    }

    @Override
    public AjaxResult deleteSingle (String[] ids) {
        boolean update = this.update ( new LambdaUpdateWrapper<KingTravelExpensesTypeEntity> ().
                set ( KingTravelExpensesTypeEntity :: getIsUsed, ConstUtil.IS_USED_NO ).
                set ( KingTravelExpensesTypeEntity :: getUpdateTime, DateUtils.getNowDate () ).
                set ( KingTravelExpensesTypeEntity :: getUpdateBy, SecurityUtils.getLoginUser ().getUser ().getUserId () ).
                in ( KingTravelExpensesTypeEntity :: getId, ids ) );
        if ( ! update ) {
            throw new CustomException ( "删除差旅费类型失败！" );
        }
        return AjaxResult.success ( "删除差旅费类型成功" );
    }

    @Override
    public AjaxResult disabled (String id) {
        KingTravelExpensesTypeEntity expensesTypeEntity = this.getById ( id );
        boolean update = this.update ( new LambdaUpdateWrapper<KingTravelExpensesTypeEntity> ().
                set ( KingTravelExpensesTypeEntity :: getIsDisabled, expensesTypeEntity.getIsDisabled () == 1 ? 2 : 1 ).
                set ( KingTravelExpensesTypeEntity :: getDisabledTime, DateUtils.getNowDate () ).
                set ( KingTravelExpensesTypeEntity :: getDisabledBy, SecurityUtils.getLoginUser ().getUser ().getUserId () ).
                eq ( KingTravelExpensesTypeEntity :: getId, id ) );
        if ( ! update ) {
            throw new CustomException ( expensesTypeEntity.getIsDisabled () == 1 ? "禁用差旅费类型失败！" : "解禁差旅费类型失败！" );
        }
        return AjaxResult.success ( expensesTypeEntity.getIsDisabled () == 1 ? "禁用差旅费类型成功" : "解禁差旅费类型成功" );
    }
}
